package com.ldj.common;
import java.awt.Color;
import java.awt.GradientPaint;
import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
public class barChartServlet extends HttpServlet {
	public void doPost( HttpServletRequest request,HttpServletResponse response )throws ServletException, IOException{		
		Hashtable rd = (Hashtable)request.getSession().getAttribute("data");
		String tag = request.getParameter("tag");
		if(tag != null && "clear".equals(tag)){
			request.getSession().removeAttribute("data");
		}
		if(rd != null){
			String title = (String)rd.get("barTitle");
			String domain = (String)rd.get("domain");
			String range = (String)rd.get("range");
	        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	        
	        List list = (List)rd.get("data");
			if(list != null && list.size() > 0){
				for(int i = 0; i < list.size(); i ++){
					Hashtable hd = (Hashtable)list.get(i);
					dataset.addValue((Number)hd.get("value"), (String)hd.get("title"), (String)hd.get("name"));
				}
			}
	        JFreeChart chart = ChartFactory.createBarChart(
	        		title,       				// chart title标题
	        		domain,               		// domain axis label横
	        		range,                  	// range axis label竖
	                dataset,                  	// data
	                PlotOrientation.VERTICAL, 	// orientation
	                true,                     	// include legend下方显示例子
	                true,                     	// tooltips?
	                false                     	// URLs?
	        );
	
	        chart.setBackgroundPaint(Color.white);
	        CategoryPlot plot = (CategoryPlot) chart.getPlot();
	        plot.setBackgroundPaint(Color.lightGray);
	        plot.setDomainGridlinePaint(Color.white);
	        plot.setDomainGridlinesVisible(true);
	        plot.setRangeGridlinePaint(Color.white);
	
	        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
	        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
	        BarRenderer renderer = (BarRenderer) plot.getRenderer();
	        renderer.setDrawBarOutline(false);
		            
	        GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.red, 
	                   0.0f, 0.0f, new Color(0, 0, 64));
	        renderer.setSeriesPaint(0, gp0);
	
	        CategoryAxis domainAxis = plot.getDomainAxis();
	        domainAxis.setCategoryLabelPositions(
	                   CategoryLabelPositions.createUpRotationLabelPositions(
	                   0));//Math.PI / 3.0
			
	        ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1, chart, 600, 420, null);
			}
	}
	
	public void doGet( HttpServletRequest request,HttpServletResponse response )throws ServletException, IOException{
		this.doPost(request, response);
	}
}
